#include "include/irq.h"
#include "include/kernel.h"

typedef struct hwndfunc{
  void (*HWND)();
  void (*HWNDPB)(BYTE);
  void (*HWNDPW)(WORD);
}hwndfunc;

static hwndfunc hwndList[MAX_HANDLER];

void addHandler(int IRQ, void *Handler)
{
  hwndList[IRQ].HWND = Handler;
}

void addHandlerPW(int IRQ, void (*Handler)(WORD))
{
  hwndList[IRQ].HWNDPW = Handler;
}

void addHandlerPB(int IRQ, void (*Handler)(BYTE))
{
  hwndList[IRQ].HWNDPB = Handler;
}

void removeHander(int IRQ)
{
  hwndList[IRQ].HWND = 0;

}

IRQ(IRQ_INT0)
{
  if (hwndList[HWND_INT0].HWND)
    hwndList[HWND_INT0].HWND();
  RETI;
}

IRQ(IRQ_INT1)
{
  if (hwndList[HWND_INT1].HWND)
    hwndList[HWND_INT1].HWND();
  RETI;
}

IRQ(IRQ_TIMER0_OVF)
{
  if (hwndList[HWND_TIMER0].HWND)
    hwndList[HWND_TIMER0].HWND(50);
  writeByte(__TCNT0__, 0);
  RETI;
}

IRQ(IRQ_TIMER1_OVF)
{
  if (hwndList[HWND_TIMER1].HWND)
    hwndList[HWND_TIMER1].HWND();
  writeByte(__TCNT1L__, 0);
  writeByte(__TCNT1H__, 255);
  RETI;
}

IRQ(IRQ_ADC)
{
  bitSet(OB, 5);
  if (hwndList[HWND_ADC0].HWND)
    hwndList[HWND_ADC0].HWND();
  RETI;
}

void enableInt()
{
  bitSet(__SREG__, __I);
  EI;
}

void disableInt()
{
  DI;
}
